package com.healthcloud.service.impl;


import com.healthcloud.dao.InstrumentMapper;
import com.healthcloud.entity.InstrumentEntity;
import com.healthcloud.service.InstrumentService;
import com.healthcloud.utils.PageParam;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sound.midi.Instrument;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by houxianghua on 2015/7/23.
 */
@Service
@Transactional
public class InstrumentServiceImpl implements InstrumentService {

    @Autowired
    private InstrumentMapper instrumentMapper;


    @Override
    public InstrumentEntity get(int id) {
        return instrumentMapper.get(id);
    }

    @Override
    public List<InstrumentEntity> findByEntityAll(InstrumentEntity entity) {
        return instrumentMapper.findByEntityAll(entity);
    }

    @Override
    public List<InstrumentEntity> search(InstrumentEntity entity, PageParam pageParam) {
        return instrumentMapper.search(entity, pageParam);
    }

    @Override
    public int count(InstrumentEntity entity) {
        return instrumentMapper.count(entity);
    }

    @Override
    public int save(InstrumentEntity entity) {
        entity.setCreatedTime(DateTime.now());
        entity.setUpdatedTime(DateTime.now());
        if(entity.getType()!=null&&entity.getType()==2){
            entity.setBluetoothName("");
        }
        return instrumentMapper.save(entity);
    }

    @Override
    public int update(InstrumentEntity entity) {
        entity.setUpdatedTime(DateTime.now());
        if(entity.getType()!=null&&entity.getType()==2){
            entity.setBluetoothName("");
        }
        return instrumentMapper.update(entity);
    }

    @Override
    public int logicalDelete(int id) {
        return instrumentMapper.logicalDelete(id);
    }

    @Override
    @Transactional
    public int logicalDeletes(int[] ids) {
        int j =0;
        for (int i=0; i<ids.length;i++){
            instrumentMapper.logicalDelete(ids[i]);
        }
        return j;
    }

    @Override
    @Transactional
    public void saveBatch(List<InstrumentEntity> entity) {
        List<InstrumentEntity> addList = new ArrayList<>();
        List<InstrumentEntity> updateList = new ArrayList<>();
        for (int i = 0; i < entity.size(); i++) {
            InstrumentEntity instrument = entity.get(i);
            if (instrument.getId() == null) {
                instrument.setCreatedTime(DateTime.now());
                instrument.setUpdatedTime(DateTime.now());
                addList.add(instrument);
            } else {
                updateList.add(instrument);
            }
        }
        if (addList.size() != 0) {
            instrumentMapper.saveBatch(addList);
        }
        for (int i = 0; i < updateList.size(); i++) {
            InstrumentEntity instrument = updateList.get(i);
            update(instrument);
        }
    }
}
